import { AddCommdity, EditCommdity, EnableCommdityParams, GetTeaListParam, SearchListQuery } from "@Tea/model"
import { renderInsertParam, renderInsertSql, renderSelectParam, renderSelectSql, renderUpdateParam, renderUpdateSql } from "../utils/renderSql"
import { ReposBase } from "./RepoBase"
import { TokenObj } from "../dao/Token"

export class CommondityRepository extends ReposBase {
  private static inst: CommondityRepository

  public static getInstance() {
    if (!CommondityRepository.inst) {
      CommondityRepository.inst = new CommondityRepository()
    }

    return CommondityRepository.inst
  }

  getPageList(params: GetTeaListParam) {
    const config: renderSelectParam = {
      database: [
        { name: "commodities", aliasName: "c" },
        { name: "tea_types", aliasName: "t" },
        { name: "users", aliasName: "u" }
      ],
      where: [
        { key: 't.type_id', sqlVal: 'c.tea_type', isDatabase: true },
        { key: 'u.user_id', sqlVal: 'c.creator', isDatabase: true },
        { key: "c.enable", sqlVal: params.enable }
      ],
      returnFeild: [
        { name: "tea_id", aliasName: "teaId" },
        { name: 'c.tea_name', aliasName: 'teaName' },
        "cover",
        { name: 't.type_name', aliasName: 'typeName' },
        { name: 'c.tea_type', aliasName: 'teaType' },
        "number",
        "price",
        { name: "u.username", aliasName: "creator" },
        { name: "create_time", aliasName: "createTime" }
      ],
      like: [
        { key: "c.tea_name", sqlVal: params.teaName }
      ]
    }

    params.teaType && config.where.push({
      key: "c.tea_type", sqlVal: params.teaType
    })

    const sql = renderSelectSql(config)
    console.log(sql);

    return this.pagingQuery(sql, params)
  }

  searchList(query: SearchListQuery) {
    const config: renderSelectParam = {
      database: [
        { name: "commodities", aliasName: "c" },
        { name: "tea_types", aliasName: "t" },
        { name: "users", aliasName: "u" }
      ],
      where: [
        { key: 't.type_id', sqlVal: 'c.tea_type', isDatabase: true },
        { key: 'u.user_id', sqlVal: 'c.creator', isDatabase: true },
        { key: "c.enable", sqlVal: 1 }
      ],
      returnFeild: [
        { name: "tea_id", aliasName: "teaId" },
        { name: 'c.tea_name', aliasName: 'teaName' },
        "cover",
        { name: 't.type_name', aliasName: 'typeName' },
        { name: 'c.tea_type', aliasName: 'teaType' },
        "number",
        "price",
        { name: "u.username", aliasName: "creator" },
        { name: "create_time", aliasName: "createTime" }
      ],
      like: [
        { key: "c.tea_name", sqlVal: query.teaName }
      ],
      otherSql: `ORDER BY price ${query.desc == 1 ? 'desc' : 'asc'}`
    }


    const sql = renderSelectSql(config)
    console.log(sql);

    return this.query(sql)
  }

  addCommdity(params: AddCommdity, hander: TokenObj['userId']) {
    const insert: renderInsertParam = {
      database: 'commodities',
      keyAndVal: [
        { key: "tea_name", value: params.teaName },
        { key: "tea_type", value: params.teaType },
        { key: "cover", value: params.cover },
        { key: "number", value: params.number },
        { key: "creator", value: hander },
        { key: "price", value: params.price },
        { key: "enable", value: 1 },
        { key: 'create_time', value: new Date() }
      ]
    }

    return this.insert(renderInsertSql(insert))
  }

  editCommdity(params: EditCommdity, hander: TokenObj['userId']) {
    const update: renderUpdateParam = {
      database: 'commodities',
      keyAndVal: [
        { key: "tea_name", value: params.teaName },
        { key: "tea_type", value: params.teaType },
        { key: "cover", value: params.cover },
        { key: "number", value: params.number },
        { key: "price", value: params.price },
        { key: "updator", value: hander },
        { key: "update_time", value: new Date() },
      ],
      where: [
        { key: 'tea_id', sqlVal: params.teaId }
      ]
    }
    const sql = renderUpdateSql(update)
    console.log("showSql:", sql);

    return this.update(sql)
  }

  enableCommdity(params: EnableCommdityParams, hander: TokenObj['userId']) {
    const update: renderUpdateParam = {
      database: 'commodities',
      keyAndVal: [
        { key: "enable", value: params.enable },
        { key: "updator", value: hander },
        { key: "update_time", value: new Date() },
      ],
      where: [
        { key: 'tea_id', sqlVal: params.teaId }
      ]
    }
    const sql = renderUpdateSql(update)
    console.log("showSql:", sql);

    return this.update(sql)
  }
}